package stock.commons;

import java.util.Scanner;

import stock.beans.Product;
import stock.database.DBValuesSetter;

import com.mysql.jdbc.Connection;

public class DBLoaderLineProcessor extends LineProcessor {
	private int fileNumber = 0;
	private String delimiter;

	public DBLoaderLineProcessor(int fileNumber, String delimiter) {
		super();
		this.fileNumber = fileNumber;
		this.delimiter = delimiter;
	}

	public void processLine(String line, Connection conn) {
		Scanner scanner = new Scanner(line);
		scanner.useDelimiter(this.delimiter);
		ProductParsedData data;
		if ((this.fileNumber == 1) || (this.fileNumber == 2)) {
			data = new StockProductParsedData();
		} else {
			data = new MovProductParsedData();
		}
		while (scanner.hasNext()) {
			data.addValue(scanner.next());
		}
		Product product = ProductBuilder.buildProduct(data);
		if (!"".equals(product.getId())) {
			String tableName = "";
			switch (this.fileNumber) {
			case (1):
				tableName = "jabbar";
				break;
			case (2):
				tableName = "bird";
				break;
			case (3):
				tableName = "jabbar2";
				break;
			case (4):
				tableName = "bird2";
				break;
			}
			DBValuesSetter inicialvalues = new DBValuesSetter();
			inicialvalues.insert("insert into " + tableName + " values (\""
					+ product.getId() + "\",\"" + product.getDescripcion()
					+ "\"," + product.getCantidad() + ")", conn);

		}
	}



}
